/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Bdd;

import java.sql.*;
import java.util.ArrayList;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 *
 * @author kazanova
 */
public abstract class Repository implements IRepository {

    ////////////////
    // Constantes //
    ////////////////
    public static final String DATABASE_DEFAULT_URL = "jdbc:mysql://iutainfosrv2.univ-lyon1.fr:3306/p0925231";
    public static final String DATABASE_DEFAULT_USER = "p0925231";
    public static final String DATABASE_DEFAULT_PSWD = "iut2011";
    public static final DateFormat formatSql = new SimpleDateFormat("yyyy-MM-dd");
    
    
    public static final int INSERT_OK = 0;
    public static final int INSERT_ERROR_EXIST = 1;
    public static final int INSERT_ERROR_VIP = 2;
    public static final int INSERT_ERROR_NET = 3;
    ///////////////
    // Attributs //
    ///////////////
    
    protected String connectionUrl;
    protected String connectionUser;
    protected String connectionPassword;
    protected Statement stmt;
    protected PreparedStatement stmtPr;
    protected ResultSet rs;
    protected Connection dataBaseConnection;

    /////////////////////////////
    // Constructeur par défaut //
    /////////////////////////////

    public Repository() {
        connectionUrl = DATABASE_DEFAULT_URL;
        connectionUser = DATABASE_DEFAULT_USER;
        connectionPassword = DATABASE_DEFAULT_PSWD;
    }

    ////////////////////////
    // Methodes publiques //
    ////////////////////////

    // Gestion des connexions
    public void openConnection() {
        try {
            this.dataBaseConnection = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
            this.stmt = dataBaseConnection.createStatement();
            System.out.println("Connexion réussie");
        } catch (SQLException e) {
            System.err.println("ERR0R MySql : " + e);
        }
    }
    public void closeConnection() {
        try {
            this.stmt.close();
            this.dataBaseConnection.close();
        } catch (SQLException e) {
            System.out.println("ERREUR MySql : " + e);
        }
    }
}
